from django.db.models import Manager



class UserScoreManage(Manager):

    def __init__(self, extra_model):
        super().__init__()
        self.extra_model = extra_model

    def create_score(self, action, *, user_id, remark, price=1, **kwargs):
        """
        :param action:
        :param user_id:
        :param remark:
        :param price: 充值的金额、其它动作不需要传入
        :param kwargs:
        :return:
        """
        if action != "充值":
            price = 1

        queryset = self.extra_model.objects.filter(action=action, is_enable=True, is_delete=False)

        if queryset.exists():
            # 获取 积分配置信息
            score_config = queryset.first()
            # 获取要赠送的积分数量
            score = score_config.score * price
            # 给 用户赠送积分 , self.model 获取 UserScore 模型类
            return self.model.objects.create(user_id=user_id, score=score, remark=remark, **kwargs)

        return None